package com.example.springbootkafka.producer;

import com.example.springbootkafka.configration.KafkaFutureCallback;
import com.example.springbootkafka.constant.KafkaConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

/**
 * InitializingBean接口为bean提供了初始化方法的方式，它只包括afterPropertiesSet方法，
 * 凡是继承该接口的类，在 初始化bean 的时候都会执行该方法。
 */
@Component
public class KafkaProducer2 implements InitializingBean {
    private static final Logger log = LoggerFactory.getLogger(KafkaProducer2.class);

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;
    @Autowired
    private KafkaFutureCallback kafkaFutureCallback;

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("这里是InitializingBean");
        kafkaTemplate.send(KafkaConstant.TOPIC1.getValue(),"这里是InitializingBean").addCallback(kafkaFutureCallback);

    }
}
